// @ts-check
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import vueParser from "vue-eslint-parser";
import eslintConfigPrettier from "eslint-config-prettier";
import pluginPrettier from "eslint-plugin-prettier";

export default tseslint.config(
    {
        // 全局忽略的文件
        ignores: [
            "dist/**",
            "dist-ssr/**",
            "coverage/**",
            "node_modules/**",
            "*.min.js",
            "public/**",
        ],
    },

    // 基础 JavaScript 配置
    {
        files: ["**/*.{js,mjs,cjs}"],
        languageOptions: {
            ecmaVersion: 2022,
            sourceType: "module",
            globals: {
                ...globals.browser,
                ...globals.node,
                ...globals.es2022,
            },
        },
        rules: {
            ...js.configs.recommended.rules,
            "no-console": "warn",
            "no-debugger": "warn",
            "no-unused-vars": ["error", {argsIgnorePattern: "^_"}],
            "prefer-const": "error",
            "no-var": "error",
        },
    },

    // TypeScript 配置
    ...tseslint.configs.recommended.map((config) => ({
        files: ["**/*.{ts,tsx,mts,cts}"],
        ...config,
        rules: {
            ...config.rules,
            "@typescript-eslint/no-unused-vars": [
                "error",
                {argsIgnorePattern: "^_"},
            ],
            "@typescript-eslint/no-explicit-any": "warn",
            "@typescript-eslint/explicit-function-return-type": "off",
            "@typescript-eslint/explicit-module-boundary-types": "off",
            "@typescript-eslint/no-empty-function": "warn",
        },
    })),

    // Vue 文件配置
    ...pluginVue.configs["flat/essential"],
    {
        files: ["**/*.vue"],
        languageOptions: {
            parser: vueParser,
            parserOptions: {
                parser: "@typescript-eslint/parser",
                extraFileExtensions: [".vue"],
                ecmaFeatures: {
                    jsx: true,
                },
            },
        },
        rules: {
            ...pluginVue.configs["flat/essential"].rules,
            "vue/multi-word-component-names": "off",
            "vue/no-v-html": "warn",
            "vue/require-default-prop": "off",
            "vue/require-explicit-emits": "error",
            "vue/no-unused-vars": "error",
            "vue/html-self-closing": [
                "error",
                {
                    html: {void: "any", normal: "any", component: "always"},
                    svg: "always",
                    math: "always",
                },
            ],
        },
    },

    // TypeScript + Vue 组合配置
    {
        files: ["**/*.vue"],
        languageOptions: {
            parser: vueParser,
            parserOptions: {
                parser: tseslint.parser,
                extraFileExtensions: [".vue"],
            },
        },
    },

    // 测试文件配置
    {
        files: ["**/__tests__/*.{j,t}s?(x)", "**/tests/unit/**/*.{j,t}s?(x)"],
        languageOptions: {
            globals: {
                ...globals.jest,
                ...globals.node,
            },
        },
        rules: {
            "no-console": "off",
            "no-debugger": "off",
        },
    },

    // Prettier 配置
    {
        plugins: {
            prettier: pluginPrettier,
        },
        rules: {
            ...eslintConfigPrettier.rules,
            "prettier/prettier": "error",
        },
    },
);
